unit entTeklifListesi;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, uClassEntry, cxGraphics, cxControls,
  cxLookAndFeels, cxLookAndFeelPainters, cxContainer, cxEdit,
  cxPCdxBarPopupMenu, cxStyles, cxCustomData, cxFilter, cxData, cxDataStorage,
  Data.DB, cxDBData, Vcl.Menus, Data.Win.ADODB, Vcl.ActnList, cxPropertiesStore,
  cxHeader, Vcl.StdCtrls, cxButtons, cxGridLevel, cxClasses, cxGridCustomView,
  cxGridCustomTableView, cxGridTableView, cxGridDBTableView, cxGrid, cxPC,
  cxGroupBox, cxTextEdit, cxMaskEdit, cxDropDownEdit, cxCalendar, cxDBEdit,
  cxButtonEdit, cxSpinEdit;

type
  TfmTeklifList = class(TfmClassEntry)
    grdReportID: TcxGridDBColumn;
    grdReportdteBelgeTarih: TcxGridDBColumn;
    grdReportsYetkili: TcxGridDBColumn;
    grdReportsFirmaAdi: TcxGridDBColumn;
    grdReportdteSonTarih: TcxGridDBColumn;
    grdReportnIskonto: TcxGridDBColumn;
    grdReportnKdv: TcxGridDBColumn;
    grdReportToplam: TcxGridDBColumn;
    grdReportGenelToplam: TcxGridDBColumn;
    cxGroupBox1: TcxGroupBox;
    Label5: TLabel;
    dtSonTarih: TcxDBDateEdit;
    edtFirmaAdi: TcxDBButtonEdit;
    Label3: TLabel;
    Label1: TLabel;
    edYetkili: TcxDBTextEdit;
    dbTarih: TcxDBDateEdit;
    Label2: TLabel;
    cxGrid1: TcxGrid;
    grdListhar: TcxGridDBTableView;
    cxGridLevel1: TcxGridLevel;
    Label4: TLabel;
    edFiyat: TcxDBSpinEdit;
    Label6: TLabel;
    edIskonto: TcxDBSpinEdit;
    Label7: TLabel;
    edtToplam: TcxDBSpinEdit;
    Label8: TLabel;
    edtGenelToplam: TcxDBSpinEdit;
    aqListhar: TADOQuery;
    dsListhar: TDataSource;
    grdListharsUrun: TcxGridDBColumn;
    grdListharsTanim: TcxGridDBColumn;
    grdListharnAdet: TcxGridDBColumn;
    grdListharnFiyat: TcxGridDBColumn;
    grdListharnSira: TcxGridDBColumn;
    aqSearch: TADOQuery;
    grdListharnToplamFiyat: TcxGridDBColumn;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure acListExecute(Sender: TObject);
    procedure acNewExecute(Sender: TObject);
    procedure acSaveExecute(Sender: TObject);
    procedure aqListharBeforePost(DataSet: TDataSet);
    procedure aqListAfterOpen(DataSet: TDataSet);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  fmTeklifList: TfmTeklifList;

implementation

{$R *.dfm}

procedure TfmTeklifList.acListExecute(Sender: TObject);
var
  ExCursor: TCursor;
begin
  inherited;
  ExCursor := Screen.Cursor;
  Screen.Cursor := crSQLWait;
  try
    With aqList do
    begin
      Close;
      Sql.Text := 'SELECT * FROM tbTeklif';
      Open;
    end;
  finally
    Screen.Cursor := ExCursor;
  end;
end;

procedure TfmTeklifList.acNewExecute(Sender: TObject);
begin
  inherited;
  aqList.FieldByName('dteBelgeTarih').Value := Date;
  aqList.FieldByName('nKdv').Value := 18;
  aqList.FieldByName('nIskonto').Value := 0;

  with aqSearch do
  begin
    Close;
    Sql.Text := 'SELECT * FROM tbTeklifFiyatTanim Order by nSira';
    Open;

    First;
    while not Eof do
    begin
      aqListhar.Append;
      aqListhar.FieldByName('TeklifID').Value := 0;
      aqListhar.FieldByName('sUrun').Value := FieldByName('sUrun').Value;
      aqListhar.FieldByName('sTanim').Value := FieldByName('sTanim').Value;
      aqListhar.FieldByName('nAdet').Value := 1;
      aqListhar.FieldByName('nFiyat').Value := FieldByName('nFiyat').Value;
      aqListhar.FieldByName('nSira').Value := FieldByName('nSira').Value;
      aqListhar.Post;

      Next;
    end;
  end;
end;

procedure TfmTeklifList.acSaveExecute(Sender: TObject);
var
  AIndex: Integer;
  Toplam: double;
begin
  AIndex := grdListhar.DataController.Summary.FooterSummaryItems.IndexOfItemLink(grdListharnToplamFiyat);

  if AIndex >= 0 then
    Toplam := grdListhar.DataController.Summary.FooterSummaryValues[AIndex]
  else
    Toplam := 0;

  if VarIsNull(Toplam) then
    Toplam := 0;

  with aqList do
  begin
    FieldByName('nToplam').Value := Toplam - FieldByName('nIskonto').AsFloat;
    FieldByName('nGenelToplam').Value := FieldByName('nToplam').AsFloat * (1 + FieldByName('nKdv').AsFloat / 100);
  end;

  aqList.AfterOpen := nil;
  aqList.AfterScroll := nil;
  try
    inherited;
  finally
    aqList.AfterOpen := aqListAfterOpen;
    aqList.AfterScroll := aqListAfterOpen
  end;

  with aqListhar do
  begin
    First;
    while not Eof do
    begin
      Edit;
      FieldByName('TeklifID').Value := aqList.FieldByName('ID').Value;
      Post;

      Next;
    end;

    UpdateBatch;
  end;
end;

procedure TfmTeklifList.aqListAfterOpen(DataSet: TDataSet);
begin
  inherited;
  with aqListhar do
  begin
    Close;
    Parameters.ParamByName('ID').Value := DataSet.FieldByName('ID').AsInteger;
    Open;
  end;
end;

procedure TfmTeklifList.aqListharBeforePost(DataSet: TDataSet);
begin
  inherited;
  DataSet.FieldByName('nToplamFiyat').Value := DataSet.FieldByName('nAdet').AsFloat * DataSet.FieldByName('nFiyat').AsFloat;
end;

procedure TfmTeklifList.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  inherited;
  Action := caFree;
  fmTeklifList := nil;
end;

end.
